﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
using zhangmenren.IService;
using zhangmenren.Models;
using zhangmenren.Models.help;
using zhangmenren.Untils;

namespace zhangmenren.Controllers
{
    public class ZMRController : Controller
    {
        private IUserService userService;
        private ICommentServic commentServic;
        private ICensusService censusService;
        public ZMRController(ICensusService _censusService,ICommentServic _commentServic, IUserService _userService)
        {
            userService = _userService;
            commentServic = _commentServic;
            censusService = _censusService;

        }
        // GET: /<controller>/
        public async Task<IActionResult> Index()
        {
            var entity = await userService.SelectTuiJia();
            if (entity.ZMRID>0)
            {
                var comments = await commentServic.FindByZMId(entity.ZMRID);
                ViewBag.Comment = comments.ToList();
                var list= await censusService.FindByUserId(entity.Id);
                ViewBag.ShouChang = list.ToList().FindAll(p => p.Type == 0).Count;
                ViewBag.GuanZhu = list.ToList().FindAll(p => p.Type == 2).Count; 
                ViewBag.DianZhan = list.ToList().FindAll(p => p.Type == 1).Count;
            }

            return View(entity);
        }


        [HttpPost]
        public async Task<JsonResult> AddComment(int zmid, string content)
        {
            var user = QTCookie.GetCookie<UserCook>(HttpContext);
            if (user == null)
            {
                return Json(new { state = "fail", msg = "请先登录" });
            }

            if (zmid != 0)
            {
                CommentEntity entity = new CommentEntity();
                entity.HeadImg = user.Head;
                entity.Content = content;
                entity.UserName = user.UserName;
                entity.ZMID = zmid;
                var result = await commentServic.Add(entity);

                return Json(new { state = "ok", msg = "评论成功" });
            }
            return Json(new { state = "faile", msg = "标识错误" });
        }

    }
}
